package com.zhenyi.zhaozheng.admin.index;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.zhaozheng.common.model.ZzActivity;
import com.zhaozheng.common.model.ZzEnroll;

import cn.hutool.core.date.DateUtil;

public class ActivityController extends ZZAdminController {

	public void list() {
		Kv kv = getSimpleKv("title", "status", "startTime");
		SqlPara sp = db.getSqlPara("ZZActivities", kv);
		Page<ZzActivity> pages = ZzActivity.dao.paginate(getPage(), getPageSize(), sp);
		setRainbow(pages);
		setItems(pages.getList());
		render("/zz/admin/activity/list.html");
	}

	public void info() {
		setReferer();
		Long activityId = getParaToLong("activityId");
		ZzActivity info = ZzActivity.dao.findById(activityId);
		if (info == null) {
			info = new ZzActivity();
			info.setStatus(0);
			info.setSort(9999);
		}else {
			String scheme = getRequest().getScheme();
			String host = getRequest().getServerName();
			String port = String.valueOf(getRequest().getServerPort());
			info.put("urlPrefix", scheme + "://" + host + (port.equals("80") ? "" : (":" + port)));
		}
		// setInfoItem(JsonKit.toJson(info));
		setData("info", info);
		render("/zz/admin/activity/info.html");
	}

	public void submit() {
		Long activityId = getParaToLong("activityId");
		String title = getPara("title", "");
		String subtitle = getPara("subtitle");
		String intro = getPara("intro");
		String image = getPara("image");
		Integer status = getParaToInt("status", 0);
		String startTime = getPara("startTime");
		String endTime = getPara("endTime");
		Integer sort = getParaToInt("sort", 9999);

		ZzActivity info = ZzActivity.dao.findById(activityId);
		if (info == null) {
			info = new ZzActivity();
			info.setTitle(title);
			info.setStatus(status);
			info.setSort(sort);
			info.setFlag(0);
			info.save();
			info.setCode(hashids.encode(info.getId()));
			info.update();
		}
		info.setTitle(title);
		info.setSubtitle(subtitle);
		info.setIntro(intro);
		info.setImage(image);
		info.setStatus(status);
		info.setStartTime(StrKit.notBlank(startTime) ? DateUtil.parse(startTime) : null);
		info.setEndTime(StrKit.notBlank(endTime) ? DateUtil.parse(endTime) : null);
		info.setSort(sort);
		renderFlag(info.update());
	}

	public void delete() {
		Long activityId = getParaToLong("activityId");
		ZzActivity info = ZzActivity.dao.findById(activityId);
		if (info == null) {
			renderError(404);
			return;
		}
		info.setFlag(1);
		renderFlag(info.update());
	}

	/**
	 * 报名列表
	 */
	public void enrollList() {
		Long activityId = getParaToLong("activityId");
		ZzActivity activity = ZzActivity.dao.findById(activityId);
		if (activity == null) {
			renderError(404);
			return;
		}
		SqlPara sp = db.getSqlPara("ZZEnrollList", Kv.by("activityId", activityId));
		Page<ZzEnroll> pages = ZzEnroll.dao.paginate(getPage(), getPageSize(), sp);
		setRainbow(pages);
		setItems(pages.getList());
		setAttr("activityTitle", activity.getTitle());
		render("/zz/admin/activity/enrollList.html");
	}
	
	public void cancelEnroll() {
		Long enrollId = getParaToLong("enrollId");
		ZzEnroll enroll = ZzEnroll.dao.findById(enrollId);
		if (enroll == null) {
			renderError(404);
			return;
		}
		renderFlag(enroll.delete());
	}

}
